<?php

namespace App\Admin\Controllers;

use App\PlatformBalanceLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PlatformBalanceLogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '对账单明细';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PlatformBalanceLog());
        $grid->model()->orderBy('count', 'desc');

        $grid->filter(function($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 设置datetime类型
            $filter->like('order_no', '业务单号');
            $filter->between('created_at', '入账时间')->datetime();
            //\r\n1-订单结算,\r\n2-微信手续费，\r\n3-订单返佣，\r\n4-上级返佣，\r\n5-平台提现，\r\n6-用户提现平台手续费，\r\n7-会费入账，\r\n8-营销支出，\r\n9-平台充值，\r\n10-提现手续费退回'11' => '营销收入'
            $filter->in('type', '类型')->multipleSelect([
                '1' => '订单结算','2' => '微信手续费','3' => '订单返佣','4' => '上级返佣','5' => '平台提现','6' => '用户提现平台手续费','7' => '会费入账','8' => '营销支出','9' => '平台充值','10' => '提现手续费退回','11' => '营销收入'
            ]);
        });

        $grid->column('type_value', '类型');
        $grid->column('created_at', '入账时间');
        $grid->column('title', '名称');
        $grid->column('order_no', '业务单号|支付流水号')->display(function($value){
            return $value.'<br>'.$this->transaction_no;
        });
        $grid->column('account', '账户')->display(function(){return '平台余额';});
        $grid->column('type', '收支类型')->display(function($value){
            if( in_array($value, [1, 6, 7, 9]) ){
                return '收入';
            }else{
                return '支出';
            }
        });
        $grid->column('amount', '额度');
        $grid->column('balance', '余额');
        // 全部关闭
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(PlatformBalanceLog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('type', __('Type'));
        $show->field('type_value', __('Type value'));
        $show->field('title', __('Title'));
        $show->field('order_no', __('Order no'));
        $show->field('transaction_no', __('Transaction no'));
        $show->field('amount', __('Amount'));
        $show->field('balance', __('Balance'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new PlatformBalanceLog());

        $form->switch('type', __('Type'))->default(1);
        $form->text('type_value', __('Type value'));
        $form->text('title', __('Title'));
        $form->text('order_no', __('Order no'));
        $form->text('transaction_no', __('Transaction no'));
        $form->decimal('amount', __('Amount'))->default(0.00);
        $form->decimal('balance', __('Balance'))->default(0.00);

        return $form;
    }
}
